package com.blog.SystemMsg;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.blog.Event.EventMsgDomain;
import com.blog.bean.domain.Comment;
import com.blog.bean.domain.DiscussPost;
import com.blog.bean.domain.User;
import com.blog.mapper.CommentMapper;
import com.blog.mapper.DiscussPostMapper;
import com.blog.mapper.MessageMapper;
import com.blog.mapper.Search.DiscussPostRepoistory;
import com.blog.mapper.UserMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;
import java.time.LocalDateTime;

/**
 * @author 任浩
 * @version 1.0
 */
@Slf4j
@Component
public class SystemMsgConsumer {

    @Autowired
    private DiscussPostMapper discussPostMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private MessageMapper messageMapper;
    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private DiscussPostRepoistory discussPostRepoistory;

    @RabbitListener(queues = "queue")
    public void consumber(Message message) throws UnsupportedEncodingException {
        String content = new String(message.getBody(), "UTF-8");
        log.info("消费的消息为："+content);
        //把json字符串转化为java对象
        EventMsgDomain eventMsgDomain = JSON.parseObject(content,EventMsgDomain.class);
        System.out.println("实体对象为：" + eventMsgDomain);
        com.blog.bean.domain.Message messageDomain = new com.blog.bean.domain.Message();

        if(eventMsgDomain.getEntity_type() == 1){  //这是点赞操作
            if(eventMsgDomain.getOperate_type().equals("帖子")){
                //将帖子的id放入帖子实体对象中。
                DiscussPost discussPost = new DiscussPost();
                discussPost.setId(eventMsgDomain.getId());

                //点赞帖子
                messageDomain.setFromId(1);  //这是系统发送的信息
                messageDomain.setConversationId("like");
                messageDomain.setStatus(0);  //0代表未读
                messageDomain.setCreateTime(LocalDateTime.now());

                //通过操作的帖子，找到操作对象的用户id
                DiscussPost discussPost1 = discussPostMapper.queryPostByPostId(discussPost);
                messageDomain.setToId(Integer.parseInt(discussPost1.getUserId()));
                //根据用户的id查找用户的姓名。
                User user = new User();
                user.setId(eventMsgDomain.getUserid());
                User check = userMapper.check(user);

                String text = "用户" + check.getUsername() +"点赞了你的帖子" +":"+eventMsgDomain.getId();
                System.out.println("系统消息存储到数据的内容为:" + text);
                messageDomain.setContent(text);

                //把数据添加进入数据库中。
                messageMapper.add(messageDomain);
            }
            else if(eventMsgDomain.getOperate_type().equals("评论")){
                //将帖子的id放入帖子实体对象中。
                Comment comment = new Comment();
                comment.setId(eventMsgDomain.getId());

                //点赞楼主评论
                messageDomain.setFromId(1);  //这是系统发送的信息
                messageDomain.setConversationId("like");
                messageDomain.setStatus(0);  //0代表未读
                messageDomain.setCreateTime(LocalDateTime.now());

                //通过操作的帖子，找到操作对象的用户id
                comment = commentMapper.queryPostByPostId(comment);
                messageDomain.setToId(comment.getUserId());
                //根据用户的id查找用户的姓名。
                User user = new User();
                user.setId(eventMsgDomain.getUserid());
                User check = userMapper.check(user);

                String text = "用户" + check.getUsername() +"点赞了你的评论" +":"+eventMsgDomain.getId();
                System.out.println("系统消息存储到数据的内容为:" + text);
                messageDomain.setContent(text);

                //把数据添加进入数据库中。
                messageMapper.add(messageDomain);
            }
            else if(eventMsgDomain.getOperate_type().equals("回复")){
                //将帖子的id放入帖子实体对象中。
                Comment comment = new Comment();
                comment.setId(eventMsgDomain.getId());

                //点赞回复
                messageDomain.setFromId(1);  //这是系统发送的信息
                messageDomain.setConversationId("like");
                messageDomain.setStatus(0);  //0代表未读
                messageDomain.setCreateTime(LocalDateTime.now());

                //通过操作的帖子，找到操作对象的用户id
                comment = commentMapper.queryPostByPostId(comment);
                messageDomain.setToId(comment.getUserId());
                //根据用户的id查找用户的姓名。
                User user = new User();
                user.setId(eventMsgDomain.getUserid());
                User check = userMapper.check(user);

                String text = "用户" + check.getUsername() +"点赞了你的回复" +":"+eventMsgDomain.getId();
                System.out.println("系统消息存储到数据的内容为:" + text);
                messageDomain.setContent(text);

                //把数据添加进入数据库中。
                messageMapper.add(messageDomain);
            }
        }
        else if(eventMsgDomain.getEntity_type() == 2){ //这是评论操作
            if(eventMsgDomain.getOperate_type().equals("发布")){

                Comment comment = new Comment();
                comment.setId(eventMsgDomain.getId());

                //发布评论
                messageDomain.setFromId(1);  //这是系统发送的信息
                messageDomain.setConversationId("comment");
                messageDomain.setStatus(0);  //0代表未读
                messageDomain.setCreateTime(LocalDateTime.now());

                //通过操作的帖子，找到操作对象的用户id
                comment = commentMapper.queryPostByPostId(comment);
                messageDomain.setToId(eventMsgDomain.getToId());

                //根据用户的id查找用户的姓名。
                User user = new User();
                user.setId(eventMsgDomain.getUserid());
                User check = userMapper.check(user);

                String text = "用户" + check.getUsername() +"在你的帖子发布了评论" +":"+eventMsgDomain.getId();
                System.out.println("系统消息存储到数据的内容为:" + text);
                messageDomain.setContent(text);

                //把数据添加进入数据库中。
                messageMapper.add(messageDomain);
            }
            else if(eventMsgDomain.getOperate_type().equals("回复")){
                //回复评论
                messageDomain.setFromId(1);  //这是系统发送的信息
                messageDomain.setConversationId("comment");
                messageDomain.setStatus(0);  //0代表未读
                messageDomain.setCreateTime(LocalDateTime.now());

                //通过操作的帖子，找到操作对象的用户id
                messageDomain.setToId(eventMsgDomain.getToId());

                //根据用户的id查找用户的姓名。
                User user = new User();
                user.setId(eventMsgDomain.getUserid());
                User check = userMapper.check(user);

                String text = "用户" + check.getUsername() +"回复了你的评论" +":"+eventMsgDomain.getId();
                System.out.println("系统消息存储到数据的内容为:" + text);
                messageDomain.setContent(text);

                //把数据添加进入数据库中。
                messageMapper.add(messageDomain);
            }
        }
        else if(eventMsgDomain.getEntity_type() == 3){  //这是关注操作
            //关注用户
            messageDomain.setFromId(1);  //这是系统发送的信息
            messageDomain.setConversationId("attention");
            messageDomain.setStatus(0);  //0代表未读
            messageDomain.setCreateTime(LocalDateTime.now());
            messageDomain.setToId(eventMsgDomain.getToId());

            //根据用户的id查找用户的姓名。
            User user = new User();
            user.setId(eventMsgDomain.getUserid());
            User check = userMapper.check(user);

            String text = "用户" + check.getUsername() +"关注了你" +":"+eventMsgDomain.getId();
            System.out.println("系统消息存储到数据的内容为:" + text);
            messageDomain.setContent(text);

            //把数据添加进入数据库中。
            messageMapper.add(messageDomain);
        }
    }

    @RabbitListener(queues = "searchQueue")
    public void searchConsumer(Message message) throws UnsupportedEncodingException {
        String content = new String(message.getBody(), "UTF-8");
        JSONObject jsonObject = JSON.parseObject(content);
        DiscussPost discussPost = jsonObject.toJavaObject(DiscussPost.class);
        System.out.println("实体对象为：" + discussPost);
        discussPostRepoistory.save(discussPost);
    }

    @RabbitListener(queues = "deleteQueue")
    public void deleteConsumer(Message message) throws UnsupportedEncodingException {
        String content = new String(message.getBody(), "UTF-8");
        int id = Integer.parseInt(content);
        discussPostRepoistory.deleteById(id);
    }

}
